package com.chenqq.camerademo.camera.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.text.TextUtils;
import android.util.Log;

import com.chenqq.camerademo.camera.helper.UsbHelperTools;

import java.lang.annotation.Target;

/**
 * USB的连接监听
 */
public class USBReceiver extends BroadcastReceiver {
    private String TAG = getClass().getSimpleName();
    public static final String ACTION_USB_PERMISSION = "com.chenqq.camerademo.USB_PERMISSION";
    private  static USBReceiver inStance;
     public static USBReceiver getInstance() {
         if (inStance==null){
             inStance=new USBReceiver();
         }
         return inStance;
     }
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            return;
        }
        if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
            //发送进行Sd卡筛选 生成节目清单（PS  单机版版本设置）
            String path = intent.getData().getPath();
            UsbHelperTools.getInStance().onMediaMounted(path);
        }
        if (Intent.ACTION_MEDIA_EJECT.equals(action)) {
            //U盘退出：
            String path = intent.getData().getPath();
            UsbHelperTools.getInStance().onMediaEJECT(path);
        }
        UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            if (device != null) {
                UsbHelperTools.getInStance().onAttach(device);
            }
        }
        if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            if (device != null) {
                UsbHelperTools.getInStance().onDettach(device);
            }
        }
        if (ACTION_USB_PERMISSION.equals(action)) {
            if (device != null) {
                boolean booleanExtra = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
                UsbHelperTools.getInStance().onPermission(device,booleanExtra);
            }
        }
    }
}